﻿using Newtonsoft.Json;
using Survey.DataAccessLayer;
using Survey.DataAccessLayer.Data;
using Survey.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using Xunit;

namespace Hello
{
    class Program
    {

        static void Main(string[] args)
        {

            //Init();
            //AddUserTest();
            //UserSelect();
            //TestLoginUser();
            //Show();
            //EditeUser();


            //TongJiShuJu();
           MoNiShuJu(); 
        }
        #region 统计数据
        public static void TongJiShuJu()
        {
            //获取问卷
            QuestionnaireAccessor questionnaireAccessor = new QuestionnaireAccessor();
            QuestionAccessor questionAccessor = new QuestionAccessor();
            foreach (var questionnaire in questionnaireAccessor.Select(q=>true))
            {

                List<IEnumerable<KeyValuePair<int, int>>> list = new List<IEnumerable<KeyValuePair<int, int>>>();
                var wentis = questionnaire.QuestionCollection;
                foreach (var wenti in wentis)
                {
                    var tongjis = questionAccessor.GetFeedbackCount(wenti.Id);
                    list.Add(tongjis);
                }
                Console.WriteLine(JsonConvert.SerializeObject(list));
                break;
            }
        }

        #endregion



        #region 模拟答卷
        //提交模拟数据
        static QuestionFeedbackAccessor questionFeedbackAccessor = new QuestionFeedbackAccessor();
        public static void MoNiShuJu()
        {
            
            QuestionnaireAccessor questionnaireAccessor = new QuestionnaireAccessor();
            foreach (var questionnaire in questionnaireAccessor.Select(q=>true))
            {
                foreach (var wenti in questionnaire.QuestionCollection)
                {
                    int opSize = wenti.QuestionOptionCollection.Count;
                    int[] opIds = new int[opSize];
                    int i = 0;
                    foreach (var xuanxiang in wenti.QuestionOptionCollection)
                    {
                        opIds[i] = xuanxiang.Id;
                        i++;
                    }

                    MoNiDiaoCha(opSize, opIds);
                }
                questionFeedbackAccessor.Save();
            }

            
        }
        //模拟1000人对问题i进行选择
        public static void MoNiDiaoCha(int opSize, int[] opIds)
        {
            Random random = new Random();
            int lenth = 1000;
            for (int start = 0; start < lenth;)
            {
                int index = random.Next() % opSize;
                int addNum = random.Next() % 50;

                for (int i = 0; i < addNum; i++)
                {
                    questionFeedbackAccessor.Add(new QuestionFeedback
                    {
                        QuestionOptionId = opIds[index]
                    });
                    Console.WriteLine("选择了：" + opIds[index]);
                }
                lenth -= addNum;
            }

        }


        #endregion

        //解决Self loop问题
        public static String Obj2Json(Object obj)
        {

            JsonSerializerSettings setting = new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                Formatting = Formatting.None
            };
            var ret = JsonConvert.SerializeObject(obj, setting);
            return ret;
        }


        #region 数据库测试
        //查询测试
        private static void Show()
        {
            QuestionnaireAccessor accessor = new QuestionnaireAccessor();
            Questionnaire questionnaire = accessor.GetQuestionnaire(1);

            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            };


            JsonSerializer ser = JsonSerializer.Create(settings);
            using (StringWriter sw = new StringWriter())
            {
                ser.Serialize(sw, questionnaire);
                Console.WriteLine("Show()" + sw != null ? "通过" : "失败");
            }

        }

        //初始化测试（向数据库添加数据）
        private static void Init()
        {
            QuestionOption option11 = new QuestionOption()
            {
                Describe = "不难"
            };
            QuestionOption option12 = new QuestionOption()
            {
                Describe = "难"
            };
            QuestionOption option21 = new QuestionOption()
            {
                Describe = "喜欢"
            };
            QuestionOption option22 = new QuestionOption()
            {
                Describe = "不喜欢"
            };
            QuestionOption option23 = new QuestionOption()
            {
                Describe = "不知道"
            };
            Question question1 = new Question()
            {
                Describe = "C++难不难",
                Type = 0,
                QuestionOptionCollection = new List<QuestionOption>()
                {
                    option11,
                    option12
                }
            };
            Question question2 = new Question()
            {
                Describe = "喜不喜欢C++",
                Type = 0,
                QuestionOptionCollection = new List<QuestionOption>()
                {
                    option21,
                    option22,
                    option23
                }
            };
            Questionnaire questionnaire = new Questionnaire()
            {
                Describe = "C++调查",
                IsPublish = false,
                Title = "C++调查",
                QuestionCollection = new List<Question>()
                {
                    question1,
                    question2
                }
            };

            List<Questionnaire> questionnaireList = new List<Questionnaire>()
                {
                    questionnaire
                };
            UserAccessor accessor = new UserAccessor();
            accessor.Add("admin@qq.com", "admin", questionnaireList);
            bool isok = accessor.Save() > 0;

            Console.WriteLine("Init()" + (isok ? "通过" : "失败"));
        }

        //添加用户测试
        private static void AddUserTest()
        {
            UserAccessor userAccessor = new UserAccessor();
            userAccessor.Add(new User
            {
                Email = "测试用户名",
                PasswordHash = "测试密码"
            });
            bool isok = userAccessor.Save() > 0;
            Console.WriteLine("AddUserTest()" + (isok ? "y通过" : "失败"));
        }
        //(非查询)修改用户测试
        private static void EditeUser()
        {
            UserAccessor userAccessor = new UserAccessor();
            User user = new User()
            {
                Id = 2,
                Email = "修改了Email",
                PasswordHash = "修改了密码",

            };
            userAccessor.Update(user);
            bool isok = userAccessor.Save() > 0;
            Console.WriteLine("EditeUser()" + (isok ? "y通过" : "失败"));
        }

        //用户查询测试
        private static void UserSelect()
        {

            UserAccessor userAccessor = new UserAccessor();
            //查询有没有Id为1的用户
            userAccessor.Select(u => u.Id == 1);
            bool isok = userAccessor.Save() > 0;
            Console.WriteLine("UserSelect()" + (isok ? "y通过" : "失败"));
        }
        #endregion

        #region 业务测试

        private static void TestLoginUser()
        {

        }
        #endregion

    }
}
